package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.*;
import com.gec.model.vo.ITripUserQueryVo;
import com.gec.system.common.Result;
import com.gec.system.mapper.ITripSightsServiceMapper;
import com.gec.system.service.*;
import com.gec.system.service.ITripSightsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author chenzhiling
 * @since 2024-01-20
 */
@RestController
@RequestMapping("/system/itrUser")
@Api(tags = "用户管理控制器")
@CrossOrigin
public class ITripUserController {
    @Autowired
    private ITripUserService iTripUserService;

    @Autowired
    private ITripCommentsService iTripCommentsService;

    @Autowired
    private ITripSpaceService iTripSpaceService;

    @Autowired
    private ITripItineraryOrderService itripItineraryOrderService;

    @Autowired
    private ITripServiceOrderService iTripServiceOrderService;

    @Autowired
    private ITripServiceService iTripServiceService;

    @Autowired
    private ITripSightsOrderService iTripSightsOrderService;
    @Autowired
    private ITripSightsService iTripSightsService;

    @Autowired
    private ITripSightsServiceMapper iTripSightsServiceMapper;
    @Autowired
    private ITripHotelOrderService iTripHotelOrderService;

    @Autowired
    private ITripHotelService iTripHotelService;

    @Autowired
    private ITripHotelRoomService iTripHotelRoomService;
    @Autowired
    private ITripItineraryService iTripItineraryService;


    // 分页 + 查询
    @ApiOperation("用户分页查询")
    @GetMapping("/{page}/{limit}")
    public Result findRoleByPageQuery(@PathVariable Long page,
                                      @PathVariable Long limit,
                                      ITripUserQueryVo userQueryVo) {


        IPage<ITripUser> page1 = new Page<>(page, limit);
        page1 = this.iTripUserService.selectPage(page1, userQueryVo);
        return Result.ok(page1);
    }

    //uni-app 唯一性验证
    @ApiOperation("根据id 去获取一个用户")
    @GetMapping("/checkOutName")
    public boolean checkOutName(@RequestParam("name") String name) {
        ITripUser it = iTripUserService.selectByName(name);
        return it == null;
    }


    // 添加
    @ApiOperation("添加修改角色")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ITripUser iTripUser) {


//        用户名唯一性判断
        ITripUser it = iTripUserService.selectByName(iTripUser.getUsername());
        if (it != null && iTripUser.getId() == null) {
            return Result.fail("name重复");
        }

        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
//            String passwordWithMD5 = MD5Helper.encrypt(sysUser.getPassword());
        String encode = bCryptPasswordEncoder.encode(iTripUser.getPassword());
        // 将加密密码设置给sysUser
        iTripUser.setPassword(encode);

        if (iTripUser.getId() == null) {
            boolean b = this.iTripUserService.save(iTripUser);
            if (b) {
                return Result.ok();
            } else {
                return Result.fail();
            }
        }
        boolean isSuccess = this.iTripUserService.saveOrUpdate(iTripUser);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //    用户注册  小程序  并放行
    @ApiOperation("添加修改角色")
    @PostMapping("/register")
    public Result register(@RequestBody ITripUser iTripUser) {
        //        用户名唯一性判断
        ITripUser it = iTripUserService.selectByName(iTripUser.getUsername());
        if (it != null) {
            return Result.fail("name重复");
        }
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(iTripUser.getPassword());
        // 将加密密码设置给sysUser
        iTripUser.setPassword(encode);
        boolean b = this.iTripUserService.save(iTripUser);

        return Result.ok();

    }

    @ApiOperation("逻辑删除接口")
    @DeleteMapping("/remove/{id}")
    public Result removeUser(@PathVariable Long id) {
        boolean isSuccess = this.iTripUserService.removeById(id);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    // 根据id 去获取一个用户
    @ApiOperation("根据id 去获取一个用户")
    @GetMapping("/findUserById/{id}")
    public Result findUserById(@PathVariable Long id) {
        ITripUser iTripUser = this.iTripUserService.getById(id);
        return Result.ok(iTripUser);
    }

    // 批量删除
    @ApiOperation("批量删除")
    @DeleteMapping("/removeBatchUserByIds")
    public Result removeBatchUserByIds(@RequestBody List<Long> ids) {
        boolean b = this.iTripUserService.removeByIds(ids);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    // 更改用户状态
    @ApiOperation("更改用户状态")
    @GetMapping("/updateStatus/{id}/{activated}")
    public Result updateStatus(@PathVariable Long id,
                               @PathVariable Integer activated) {
        this.iTripUserService.updateActivated(id, activated);
        return Result.ok();
    }

    /**
     * uni-app
     */

    //用户取消订单or支付订单
    @ApiOperation(value = "取消或支付 出行订单")
    @PostMapping("/ITI")
    public Result ITI(Long id, Integer isPaid, Long initId) {
        if (isPaid == 1) {//取消订单,恢复票数
            //通过行程订单id获取 行程订单 -- 用户所选票数
            ITripItineraryOrder it = itripItineraryOrderService.getById(id);
            Long ticketNumber = it.getNumber();
            //通过行程id获取 行程 -- 行程剩余票数
            ITripItinerary iti = iTripItineraryService.getById(initId);
            Long number = iti.getNumber();
            iti.setNumber(ticketNumber + number);
            iTripItineraryService.saveOrUpdate(iti);
        }
        itripItineraryOrderService.updateIsPaid(id, isPaid);
        return Result.ok();
    }

    @ApiOperation(value = "取消或支付 景点订单")
    @PostMapping("/ITS")
    public Result ITS(Long id, Integer isPaid, Long sigId) {
        if (isPaid == 1) {//取消订单,恢复票数
            //通过景点订单id获取 景点订单 -- 用户所选票数
            ITripSightsOrder it = iTripSightsOrderService.getById(id);
            Long ticketNumber = it.getNumber();
            //通过景点id获取 行程 -- 景点剩余票数
            ITripSights its = iTripSightsService.getById(sigId);
            Long number = Long.valueOf(its.getNumber());
            its.setNumber(ticketNumber + number);
            iTripSightsService.saveOrUpdate(its);
        }
        itripItineraryOrderService.updateIsPaid(id, isPaid);
        return Result.ok();
    }

    @ApiOperation(value = "取消或支付 套餐订单")
    @PostMapping("/ITC")
    public Result ITC(Long id, Integer isPaid) {
        itripItineraryOrderService.updateIsPaid(id, isPaid);
        return Result.ok();
    }


    @ApiOperation(value = "取消或支付 酒店订单")
    @PostMapping("/ITH")
    public Result ITH(Long id, Integer isPaid) {
        if (isPaid == 1) {//取消订单,恢复票数
            //通过酒店订单获取订单所有信息 -- 用户所选票数
            ITripHotelOrder it = iTripHotelOrderService.getById(id);
            //获取该订单中酒店房间的所有id
            Long i = it.getRoomId();

            //获取每个房间用户选择的票数
            Long ticketNumber = iTripHotelOrderService.getNumberByRoomId(i);
            //通过房间id查找房间现在剩余的票数
            ITripHotelRoom itr = iTripHotelRoomService.getById(i);
            Long number = itr.getNumber();
            itr.setNumber(number + ticketNumber);
            iTripHotelRoomService.saveOrUpdate(itr);

        }
        itripItineraryOrderService.updateIsPaid(id, isPaid);
        return Result.ok();
    }

//    用户空间与评论

    //    用户发表空间

    @PostMapping("/addSpace")
    public Result addSpace(@RequestBody ITripSpace iTripSpace) {

        boolean res = this.iTripSpaceService.save(iTripSpace);
        if (res) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //    用户发布评论
    @PostMapping("/addComments")
    public Result addComments(@RequestBody ITripComments iTripComments) {
        boolean res = this.iTripCommentsService.save(iTripComments);
        if (res) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


//    //    用户点进空间，获取该空间所有评论
//    @GetMapping("/goSpace")
//    public Result goSpace(Long userId ,Long spaceId) {
//        iTripCommentsService.getBySpaceId(spaceId);
//
//        return Result.ok();
//
//    }


//    --------------------用户查询自己的订单-----------------

    //    全部订单
    @ApiOperation("获取全部订单")

    @GetMapping("/findAllOrder/{id}")
    public Result findAllOrder(@PathVariable Long id) {
//        获取出行方式订单
        List<ITripItineraryOrder> list1 = itripItineraryOrderService.getByUserId(id);
//        获取套餐订单
        List<ITripServiceOrder> list2 = iTripServiceOrderService.getByUserId(id);
//        获取景点订单
        List<ITripSightsOrder> list3 = iTripSightsOrderService.getByUserId(id);
//        获取酒店订单(后面补)
        List<ITripHotelOrder> list4 = iTripHotelOrderService.getByUserId(id);
//        放入map集合
        Map<String, Object> map = getMap(list1, list2, list3, list4);
        return Result.ok(map);
    }

//    @ApiOperation("获取全部订单")
//
//    @GetMapping("/findAllOrder")
//    public Result findAllOrder() {
////        获取出行方式订单
//        List<ITripItineraryOrder> list1 = itripItineraryOrderService.list();
////        获取套餐订单
//        List<ITripServiceOrder> list2 = iTripServiceOrderService.list();
////        获取景点订单
//        List<ITripSightsOrder> list3 = iTripSightsOrderService.list();
////        获取酒店订单(后面补)
//        List<ITripHotelOrder> list4 = iTripHotelOrderService.list();
////        放入map集合
//        Map<String, Object> map = getMap(list1, list2, list3, list4);
//        return Result.ok(map);
//    }

    private Map<String, Object> getMap(List<ITripItineraryOrder> list1, List<ITripServiceOrder> list2, List<ITripSightsOrder> list3, List<ITripHotelOrder> list4) {
        Map<String, Object> map = new HashMap<>();
        map.put("itineraryOrder", list1);
        map.put("serviceOrder", list2);
        map.put("sightsOrder", list3);
        map.put("hotelOrder", list4);
        return map;
    }
//    未出行订单  出行方面  查询所有出行时间小于现在时间的订单

    @ApiOperation("获取为出行")
    @GetMapping("/findNotOrder/{userId}")
    public Result findNotOrder(@PathVariable Long userId) {
        List<ITripItineraryOrder> it = itripItineraryOrderService.getByNewDate(userId);
        return Result.ok(it);
    }
//    未支付、未点评

    @ApiOperation("获取未支付or未点评")
    @GetMapping("/findNotOrNot/{isPaid}")
    public Result findNotOrNot(@PathVariable Long isPaid) {
//        获取出行方式订单
        List<ITripItineraryOrder> list1 = itripItineraryOrderService.getByIsPaid(isPaid);
//        获取套餐订单
        List<ITripServiceOrder> list2 = iTripServiceOrderService.getByIsPaid(isPaid);
//        获取景点订单
        List<ITripSightsOrder> list3 = iTripSightsOrderService.getByIsPaid(isPaid);
//            获取酒店订单
        List<ITripHotelOrder> list4 = iTripHotelOrderService.getByIsPaid(isPaid);
        Map<String, Object> map = getMap(list1, list2, list3, list4);
        return Result.ok(map);
    }
}

